package com.test.demo.base

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import com.example.common_library.utils.ScreenUtil
import com.hjq.toast.ToastUtils
import com.lzy.okgo.OkGo
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.mmkv.MMKV
import com.test.demo.BuildConfig
import com.test.demo.utils.ImageUtils
import com.test.demo.utils.MyExceptionHandler
import com.test.demo.utils.glide.GlideLoader
import me.jessyan.autosize.AutoSizeConfig

/**
 * Created by Tany on 2021/10/21.
 * Desc:
 *那么你可以复制该类的代码到你的自定义Application中去，然后可以自己写获取ViewModel的拓展函数即 :
 * GetViewModelExt类的getAppViewModel方法
 */
class BaseApplication : Application(), ViewModelStoreOwner {

    private lateinit var mAppViewModelStore: ViewModelStore

    private var mFactory: ViewModelProvider.Factory? = null

    companion object {
        // 全局Context
        @SuppressLint("StaticFieldLeak")
        lateinit var context: Context

        @SuppressLint("StaticFieldLeak")
        lateinit var application: BaseApplication

    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        context = base
        application = this
    }

    override fun onCreate() {
        super.onCreate()
        MMKV.initialize(this.filesDir.absolutePath + "/mmkv")
        //没有特殊需要的话,最简单的初始化就可以了
        OkGo.getInstance().init(this)
        ScreenUtil.init(this)
        ToastUtils.init(this)
//        initAutoSize()
        Thread.setDefaultUncaughtExceptionHandler(MyExceptionHandler(this))
        //bugly线上监控
        CrashReport.initCrashReport(applicationContext, "5d2c317143", false)
        ImageUtils.init(this, GlideLoader())
//        registerActivityLifecycleCallbacks(ActivityLifecycle())
//        AppManager.getAppManager().init(this)

        // 初始化SDK，Appid等信息在清单中配置
//        val sparkChainConfig = SparkChainConfig.builder()
//        sparkChainConfig.appID("6906723e")
//            .apiKey("d964f334cbbb2c29908b65ed4a6db4ce")
//            .apiSecret("ZmE2YWYyMzM0YTdmZDY3MzJkZjliNDBl") //应用申请的appid三元组
//            //                .uid(getAndroidId())
//            .logLevel(666)
//
//        val ret = SparkChain.getInst().init(applicationContext, sparkChainConfig)
//        val result: String
//        if (ret == 0) {
//            result = "SDK初始化成功,请选择相应的功能点击体验。"
//        } else {
//            result = "SDK初始化失败,错误码:$ret"
//        }
//        LogUtils.d(">>>>>${result}")

    }

    private fun initAutoSize() {
        AutoSizeConfig.getInstance()
            .setDesignWidthInDp(1920)  //设置设计图的宽度，单位dp
            .setDesignHeightInDp(1080) //设置设计图的高度，单位dp
            .setExcludeFontScale(true) //是否排除系统字体的影响
            .setUseDeviceSize(false)  //是否使用机型自带的字体配置
            .setVertical(false) //设置为横向
            .setLog(BuildConfig.DEBUG).isCustomFragment = true //是否支持自定义适配Fragment

    }

    /**
     * 获取一个全局的ViewModel
     */
    fun getAppViewModelProvider(): ViewModelProvider {
        return ViewModelProvider(this, this.getAppFactory())
    }

    private fun getAppFactory(): ViewModelProvider.Factory {
        if (mFactory == null) {
            mFactory = ViewModelProvider.AndroidViewModelFactory.getInstance(this)
        }
        return mFactory as ViewModelProvider.Factory
    }

    override fun getViewModelStore(): ViewModelStore {
        return mAppViewModelStore
    }


}